package com.s.rpc.channel;

import com.s.rpc.serializer.Serializer;
import com.s.rpc.domain.RpcResponse;
import com.s.rpc.handler.RpcClientHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ShenshenLuo
 * @email luoshenshen@buaa.edu.cn
 * @date 2025/5/11 13:54
 **/
@Setter
@Getter
public class ClientChannel extends ChannelInitializer<SocketChannel> {

    private ConcurrentHashMap<String, CompletableFuture<RpcResponse>> pendingRequests = new ConcurrentHashMap<>();

    private Serializer serializer;

    public ClientChannel(ConcurrentHashMap<String, CompletableFuture<RpcResponse>> pendingRequests) {
        this.pendingRequests = pendingRequests;
    }

    public ClientChannel(ConcurrentHashMap<String, CompletableFuture<RpcResponse>> pendingRequests, Serializer serializer) {
        this.pendingRequests = pendingRequests;
        this.serializer = serializer;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new RpcClientHandler(pendingRequests));
    }
}
